package com.gydz.gydzbaseframeworkboot.CsdE.controller;

import com.gydz.gydzbaseframeworkboot.Common.utils.export.ExcelUtil;
import com.gydz.gydzbaseframeworkboot.Common.utils.page.PageParam;
import com.gydz.gydzbaseframeworkboot.Common.utils.page.PageUtils;
import com.gydz.gydzbaseframeworkboot.Common.utils.page.TableDataInfo;
import com.gydz.gydzbaseframeworkboot.Common.utils.result.ResultUtils;
import com.gydz.gydzbaseframeworkboot.Common.utils.sqlUtil.domain.TableSelect;
import com.gydz.gydzbaseframeworkboot.Common.utils.sqlUtil.service.SqlUtilsService;
import com.gydz.gydzbaseframeworkboot.CsdE.domain.EInterfaceCsd;
import com.gydz.gydzbaseframeworkboot.CsdE.service.IEInterfaceCsdService;
import com.gydz.gydzbaseframeworkboot.SystemControl.utils.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/EInterfaceCsd")
public class EInterfaceCsdController {

    private static final Logger log = LoggerFactory.getLogger(EInterfaceCsdController.class);

    @Autowired
    private IEInterfaceCsdService eInterfaceCsdService;

    @Autowired
    private SqlUtilsService sqlUtilsService;

    @ResponseBody
    @Log(remark = "查询E接口业务")
    @PostMapping("/list")
    public void list(HttpServletResponse httpServletResponse, @ModelAttribute("priInterfaceCsd") EInterfaceCsd priInterfaceCsd) throws IOException{

        TableSelect tableSelect = new TableSelect();
        tableSelect.setTableSchema("csd_e");
        tableSelect.setPrefix("e_csd_");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        if(priInterfaceCsd.getStartTime() == null){
            ResultUtils result =  ResultUtils.fail("查询E接口业务失败,请选择查询时间范围");
            ResultUtils.responseResult(httpServletResponse,result);
            return ;
        }else {
            tableSelect.setDateMin(dateFormat.format(priInterfaceCsd.getStartTime()));
        }
        if(priInterfaceCsd.getEndTime() == null){
            priInterfaceCsd.setEndTime(new Date());
        }
        tableSelect.setDateMax(dateFormat.format(priInterfaceCsd.getEndTime()));

        List<String> tableList = sqlUtilsService.selectTabeName(tableSelect);
        if(tableList.isEmpty()){
            ResultUtils result =  ResultUtils.fail("没有该时间范围的表");
            ResultUtils.responseResult(httpServletResponse,result);
            return ;
        }


        PageUtils.startPage(new PageParam(priInterfaceCsd.getiDisplayStart(),priInterfaceCsd.getiDisplayLength(),priInterfaceCsd.getsSortDir(),priInterfaceCsd.getsSortCol()));
        priInterfaceCsd.setTableNameList(tableList);
        List<EInterfaceCsd> list = eInterfaceCsdService.selectEInterfaceCsdList(priInterfaceCsd);
        TableDataInfo tableDataInfo = PageUtils.getDataTable(list);

        ResultUtils result =  ResultUtils.succ(200,"查询E接口业务成功",tableDataInfo);
        ResultUtils.responseResult(httpServletResponse,result);
    }

    @ResponseBody
    @Log(remark = "导出E接口业务")
    @RequestMapping ("/exportData")
    public void exportData(HttpServletRequest request, @ModelAttribute("priInterfaceCsd") EInterfaceCsd priInterfaceCsd, HttpServletResponse response) throws IOException{

        TableSelect tableSelect = new TableSelect();
        tableSelect.setTableSchema("csd_e");
        tableSelect.setPrefix("e_csd_");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        if(priInterfaceCsd.getStartTime() == null){
            ResultUtils result =  ResultUtils.fail("查询E接口业务失败,请选择查询时间范围");
            ResultUtils.responseResult(response,result);
            return ;
        }else {
            tableSelect.setDateMin(dateFormat.format(priInterfaceCsd.getStartTime()));
        }
        if(priInterfaceCsd.getEndTime() == null){
            priInterfaceCsd.setEndTime(new Date());
        }
        tableSelect.setDateMax(dateFormat.format(priInterfaceCsd.getEndTime()));

        List<String> tableList = sqlUtilsService.selectTabeName(tableSelect);
        if(tableList.isEmpty()){
            ResultUtils result =  ResultUtils.fail("没有该时间范围的表");
            ResultUtils.responseResult(response,result);
            return ;
        }

        priInterfaceCsd.setsSortCol("");
        priInterfaceCsd.setTableNameList(tableList);
        List<EInterfaceCsd> list = eInterfaceCsdService.selectEInterfaceCsdList(priInterfaceCsd);

        String[] excelHeader = {"序号", "触发时间", "MSISDN", "IMSI", "机车号", "CTCS_ID", "车次", "RBC号码", "线路","小区",
                "公里标","传送方向","CRC校验","数据类型","链路层","网络层","传输层","安全层","应用层","发送T_Train",
                "确认T_Train","上行帧序号","下行帧序号","RR帧类型","拆链原因","LRBG","速度(km/h)","运行模式","设备号","端口号",
                "时隙号","子时隙号","数据长度","数据内容","PRI口索引号"};
        String[] ds_titles = {"index", "RecTime", "MsIsdn", "Imsi", "LocoNum", "CtcsId", "TrainNum", "RbcNum", "LineName", "LacCiName",
                "Kilo", "dataDir", "Crc", "C3DataType", "HdlcMsgType", "NpduMsgType", "TpduMsgType", "SapduMsgType", "ApduMsgType", "ApduTTrain",
                "ApduTTrainCfm", "HdlcNs", "HdlcNr", "HdlcPF", "Q931ReleaseCause", "LrBgIdName", "Speed", "C3Mode", "DevNum", "PortNum",
                "SlotNum","SubSlotNum", "DataLen", "DataContent", "CdrIndexPri"};
        int[] widths = {256 * 14, 256 * 24, 256 * 24, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19,
                256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19,256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19,
                256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19, 256 * 19,
                256 *19, 256 * 19, 256 * 19, 256 * 19, 256 * 19};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2,2,
                2, 2, 2, 2, 2, 2, 2, 2, 2,2,
                2, 2, 2, 2, 2, 2, 2, 2, 2,2,
                2, 2, 2, 2, 2};
        try {
            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            if (list != null && !list.isEmpty()) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                int index = 1;
                for (EInterfaceCsd rdata : list) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("index", index);
                    map.put("RecTime", rdata.getRecTime() != null ? format.format(rdata.getRecTime()) : "");
                    map.put("MsIsdn", rdata.getMsIsdn());
                    map.put("Imsi", rdata.getIMSI());
                    map.put("LocoNum", rdata.getLocoNum());
                    map.put("CtcsId", rdata.getCtcsId());
                    map.put("TrainNum", rdata.getTrainNum());
                    map.put("RbcNum", rdata.getRbcNum());
                    map.put("LineName", rdata.getLineName());
                    map.put("LacCiName", rdata.getLacCiName());
                    map.put("Kilo", rdata.getKilo());
                    map.put("dataDir", rdata.getDataDirStr());
                    map.put("Crc", rdata.getCrcStr());
                    map.put("C3DataType", rdata.getC3DataTypeStr());
                    map.put("HdlcMsgType", rdata.getHdlcMsgTypeStr());
                    map.put("NpduMsgType", rdata.getNpduMsgTypeStr());
                    map.put("TpduMsgType", rdata.getTpduMsgTypeStr());
                    map.put("SapduMsgType", rdata.getSapduMsgTypeStr());
                    map.put("ApduMsgType", rdata.getApduMsgType());
                    map.put("ApduTTrain", rdata.getApduTTrain());
                    map.put("ApduTTrainCfm", rdata.getApduTTrainCfm());
                    map.put("HdlcNs", rdata.getHdlcNs());
                    map.put("HdlcNr", rdata.getHdlcNr());
                    map.put("HdlcPF", rdata.getHdlcPF());
                    map.put("Q931ReleaseCause", rdata.getQ931ReleaseCauseStr());
                    map.put("LrBgIdName", rdata.getLRBGIDName());
                    map.put("Speed", rdata.getSpeed());
                    map.put("C3Mode", rdata.getC3ModeStr());
                    map.put("DevNum", rdata.getDevNum());
                    map.put("PortNum", rdata.getPortNum());
                    map.put("SlotNum", rdata.getSlotNum());
                    map.put("SubSlotNum", rdata.getSubSlotNum());
                    map.put("DataLen", rdata.getDataLen());
                    map.put("DataContent", rdata.getDataContent());
                    map.put("CdrIndexPri", rdata.getCdrIndexPri());
                    index++;
                    data.add(map);
                }
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            ExcelUtil.export("E接口业务数据"+format.format(new Date()), "数据列表", excelHeader, ds_titles, ds_format, widths, data, request, response);
        } catch (Exception e) {
            log.error("导出数据异常", e);
        }

    }
}
